using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace G4KFramework.Objects
{
    public class GSpriteView : GObjectView
    {
        //---------------------------------------------------------
        // Field
        private uint _column = 0;
        private uint _row = 0;
        private uint _currentFrame = 0;
        private uint _beginFrame = 0;
        private uint _endFrame = 0;

        //---------------------------------------------------------
        // Properties
        public uint row
        {
            get { return _row; }
            set { _row = value; }
        }
        public uint column
        {
            get { return _column; }
            set { _column = value; }
        }
        public uint beginFrame
        {
            get { return _beginFrame; }
            set { _beginFrame = value; }
        }
        public uint endFrame
        {
            get { return _endFrame; }
            set { _endFrame = value; }
        }
        public uint currentFrame
        {
            get { return _currentFrame; }
            set { _currentFrame = value; }
        } 

        //---------------------------------------------------------
        // Construction
        public GSpriteView(SpriteBatch spriteBatch) : base(spriteBatch)
        {

        }

        public new void Draw(Texture2D texture)
        {
            if (_row == 0 || _column == 0)
                base.Draw(texture);
            else
            {
                int width = (int)(texture.Width / _column);
                int height = (int)(texture.Height / _row);
                int y = (int)(height * _currentFrame / _column);
                int x = (int)(width * (currentFrame % _column));
                
                if (dimention.X != 0 || dimention.Y != 0)
                    _spriteBatch.Draw(texture, new Rectangle((int)_position.X, (int)_position.Y, (int)dimention.X, (int)dimention.Y), new Rectangle((int)x, (int)y, (int)width, (int)height), Color.White, _rotation, _origin, SpriteEffects.None, _layerDepth);
                else
                    _spriteBatch.Draw(texture, _position, new Rectangle((int)x, (int)y, (int)width, (int)height), Color.White, _rotation, _origin, _scale, SpriteEffects.None, _layerDepth);
            }
        }

        public void Next()
        {
            if (++_currentFrame > _endFrame)
                _currentFrame = _beginFrame;            
        }

        public void Back()
        {
            if (++_currentFrame < _beginFrame)
                _currentFrame = _endFrame;
        }
    }
}
